﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataCentral.Dataset;
using CommonData;

namespace DataCentral.Forms
{
    public partial class TablesConfigurationForm
    {
        private void SynchronizeFromSourceToDest(string sourceConn, string destConn,string sourceTable, string destTable)
        {
            //Dong bo lan dau
            if (rdgSynType.SelectedIndex == 0)
            {
                //Xoa het du lieu cu
                if (chkClearBeforeSync.Checked)
                {
                    
                }
                else
                {

                }
            }
            else
            {

            }
        }

        private DataTable GetAllSourceData(string sourceConn,string sourceTable)
        {
            string strSelect = string.Empty;
            foreach (DataCentralDataset.TablesConfigurationDetailRow drConfigDetail in _DSCental.TablesConfigurationDetail)
            {
                if (!string.IsNullOrEmpty(drConfigDetail.SourceField) && !string.IsNullOrEmpty(drConfigDetail.DestField))
                {
                    strSelect += string.Format("{0},", drConfigDetail);
                }
            }
            //Xoa dau "," cuoi cung
            strSelect = strSelect.Substring(0, strSelect.Length - 1);

            string strSQL = string.Format("SELECT {0} FROM {1}", strSelect, sourceTable);

            CommonDataAccess cmDataAccess = new CommonDataAccess();
            DataTable tblSource = cmDataAccess.GetDataTableBySQL(sourceConn, strSQL, null);
            return tblSource;
        }

        private DataTable GetDataByCondition(string sourceConn, string sourceTable, string fieldChanged, DateTime fromDate, DateTime toDate)
        {
            string strSelect = string.Empty;
            foreach (DataCentralDataset.TablesConfigurationDetailRow drConfigDetail in _DSCental.TablesConfigurationDetail)
            {
                if (!string.IsNullOrEmpty(drConfigDetail.SourceField) && !string.IsNullOrEmpty(drConfigDetail.DestField))
                {
                    strSelect += string.Format("{0},", drConfigDetail);
                }
            }
            //Xoa dau "," cuoi cung
            strSelect = strSelect.Substring(0, strSelect.Length - 1);

            string strSQL = string.Format("SELECT {0} FROM {1} WHERE {2} BETWEEN {3} AND {4}", strSelect, sourceTable,fieldChanged,fromDate,toDate);

            CommonDataAccess cmDataAccess = new CommonDataAccess();
            DataTable tblSource = cmDataAccess.GetDataTableBySQL(sourceConn, strSQL, null);
            return tblSource;
        }
    }
}
